//测试环境
//var shareUrl = "http://121.43.104.166:8085/news/share?newsId=";
//var baseUrl = "http://121.43.104.166:8085";

//正式环境
var shareUrl = "http://app.dongxihu.gov.cn/news/share?newsId=";
var baseUrl = "http://app.dongxihu.gov.cn";
var user_token = "user_token";
var UNITS = {
	'年': 31557600000,
	'月': 2629800000,
	'天': 86400000,
	'小时': 3600000,
	'分钟': 60000,
	'秒': 1000
};

//毫秒换算
function Humanize(milliseconds) {
	var humanize = '';
	$.each(UNITS, function(unit, value) {
		if (milliseconds >= value) {
			humanize = Math.floor(milliseconds / value) + unit + '前';
			return false;
		}
		return true;
	});
	return humanize || '刚刚';
}

//图片自适应高度
function AutoHeight(element) {
	var width = element.offsetWidth;
	var height = width * 0.72;
	return height + "px";
}

//数组去重
function Unique(arr) {
	// 遍历arr，把元素分别放入tmp数组(不存在才放)
	var tmp = [];
	for (var i = 0; i < arr.length; i++) {
		//该元素在tmp内部不存在才允许追加
		if (tmp.indexOf(arr[i]) === -1) {
			tmp.push(arr[i]);
		}
	}
	return tmp;
}

/*var brightness;
//显示遮罩
function cover(brightness) {
	if (typeof(div) == 'undefined') {
		div = document.createElement('div');
		div.setAttribute('style', 'position:fixed;top:0;left:0;outline:5000px solid;z-index:99999;');
		document.body.appendChild(div);
	} else {
		div.style.display = '';
	}
	div.style.outlineColor = 'rgba(0,0,0,' + brightness + ')';
}

//夜间模式
function nightMode() {
	var modeFlag = localStorage.getItem("modeFlag");
	var img = null;
	if (document.getElementById("imageDetail")) {
		return;
	}
	if (modeFlag) {
		cover(brightness = 0.5);
		localStorage.setItem("modeFlag", "true");
	} else {
		cover(brightness = 0);
		localStorage.removeItem("modeFlag");
	}
}

//判断是否是夜间模式
nightMode();*/

//是否登录
function isLogin() {
	var userToken = plus.storage.getItem(user_token);
	if (userToken && userToken !== "") {
		return true;
	} else {
		return false;
	}
}

//自动弹出软键盘
function openSoftKeyboard(ipt){
    if (mui.os.ios) {
        var webView = plus.webview.currentWebview().nativeInstanceObject();
        webView.plusCallMethod({"setKeyboardDisplayRequiresUserAction":false});
        document.getElementById(ipt).focus();
    }
    else{
        var Context = plus.android.importClass("android.content.Context");
        var InputMethodManager = plus.android.importClass("android.view.inputmethod.InputMethodManager");
        var main = plus.android.runtimeMainActivity();
        var imm = main.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0,InputMethodManager.SHOW_FORCED);
        document.getElementById(ipt).focus();
    }
}

//日期格式化函数+调用方法
Date.prototype.format = function(format) {
	var o = {
		"M+": this.getMonth() + 1, //month
		"d+": this.getDate(), //day
		"h+": this.getHours(), //hour
		"m+": this.getMinutes(), //minute
		"s+": this.getSeconds(), //second
		"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
		"S": this.getMilliseconds() //millisecond
	};
	if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
		(this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o) {
		if (new RegExp("(" + k + ")").test(format))
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
	}
	return format;
};

//无网络时提示
function throwGetNewsError() {
	mui.plusReady(function() {
		mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
		mui('#pullrefresh').pullRefresh().endPullupToRefresh(true); //参数为true代表没有更多数据了
		mui.toast("网络不给力");
	});
}

//高亮文本
function highlight(text, words, tag) {
	tag = tag || 'strong';
	var i, len = words.length,
		re;
	for (i = 0; i < len; i++) {
		re = new RegExp(words[i], 'g');
		if (re.test(text)) {
			text = text.replace(re, '<' + tag + ' style="color: red;">$&</' + tag + '>');
		}
	}

	return text;
}

//取消高亮文本
function unhighlight(text, tag) {
	tag = tag || 'strong';
	var re = new RegExp('(<' + tag + '.+? style="color: #000;">|<\/' + tag + '>)', 'g');
	return text.replace(re, '');
}

//显示图片
function showImg(parent){
	var imgs = document.querySelector(parent).querySelectorAll("img");
	for (var i = 0; i < imgs.length; ++i) {
	  imgs[i].style.display = "block";
	}
}

//隐藏图片
function hideImg(parent){
	var imgs = document.querySelector(parent).querySelectorAll("img");
	for (var i = 0; i < imgs.length; ++i) {
	  imgs[i].style.display = "none";
	}
}